package com.qiniu.githubdata.controller;

import com.qiniu.githubdata.common.Result;
import com.qiniu.githubdata.entity.Developer;
import com.qiniu.githubdata.entity.dto.ContributionResponse;
import com.qiniu.githubdata.entity.dto.DeveloperSearchResponse;
import com.qiniu.githubdata.frontend.PageResult;
import com.qiniu.githubdata.entity.dto.DeveloperDetailResponse;
import com.qiniu.githubdata.service.DeveloperService;
import com.qiniu.githubdata.service.DeveloperTagService;
import com.qiniu.githubdata.service.ThirdPartyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import javax.annotation.Resource;

//package com.qiniu.githubdata.controller;
//
///**
// * @Author: mayixiang
// * @Date: 2024-10-30 19:40
// * @Description:
// */
@CrossOrigin
@RestController
@RequestMapping("/developer")
public class DeveloperController {

    @Resource
    private DeveloperService developerService;
    @Autowired
    private DeveloperTagService developerTagService;
    @Resource
    private ThirdPartyService thirdPartyService;
    @GetMapping("/detail/{id}")
    public Result<DeveloperDetailResponse> getDeveloperInfo(@PathVariable long id) {
//        DeveloperDetailResponse developerInfo = new DeveloperDetailResponse();
//        Developer developer = developerService.getById(id);
        DeveloperDetailResponse developerDetail = developerService.getDeveloperDetail(id);
        return Result.success(developerDetail);
    }
//    @GetMapping("/search")
//    public Result<PageResult<Developer>> searchDevelopers(
//            @RequestParam(required = false) String q,
//        @RequestParam(required = false) List<String> tags,
//        @RequestParam(required = false) String domain,
//        @RequestParam(required = false) String nation,
//        @RequestParam(defaultValue = "talent_rank") String sortBy,
//        @RequestParam(defaultValue = "1") Integer page,
//        @RequestParam(defaultValue = "10") Integer pageSize
//    ) {
//        List<Developer> developers = developerService.searchDevelopers(q,tags,domain, nation, sortBy, page, pageSize);
//        return Result.success(new PageResult<>(developers, developers.size(), page, pageSize));
//    }
//

    @GetMapping("/search")
    public Result<PageResult<DeveloperSearchResponse>> searchDevelopers(
            @RequestParam(required = false) String q,
//        @RequestParam(defaultValue = "talent_rank") String sortBy,
        @RequestParam(defaultValue = "1") Integer page,
        @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        List<DeveloperSearchResponse> developers = developerService.searchResponse(q, page, pageSize);
        return Result.success(new PageResult<>(developers, developers.size(), page, pageSize));
    }
    @GetMapping("/{id}/talent-rank")
    public Result<Double> getTalentRank(@PathVariable long id) {
        Developer developer = developerService.updateTalentRank(id);
        return Result.success(developer.getTalentRank());
//        return Result.success(null);
    }
//
//    @GetMapping("/tag/{id}")
//    public Result getTagById(@PathVariable long id) {
//        return Result.success(developerTagService.getTagsByDeveloperId(id));
//    }
//
    @GetMapping("/contributions/{userId}/{year}")
    public Result<ContributionResponse> getUserContributions(
            @PathVariable String userId,
            @PathVariable int year) {
        try {
//            Developer user = developerService.getDeveloperByLogin(userId);

            ContributionResponse contributions = thirdPartyService.fetchGitHubContributions(userId,year);
            return Result.success(contributions);
        } catch (Exception e) {
            return Result.error(500, "Failed to fetch contributions: " + e.getMessage());
        }
    }
}